package com.xh.bussiness.teachbook.service.impl;

import com.xh.bussiness.exam.model.UserErrorQuestion;
import com.xh.bussiness.exam.service.IUserErrorQuestionService;
import com.xh.bussiness.teachbook.bean.dto.*;
import com.xh.bussiness.teachbook.constant.TeachingBookConstant;
import com.xh.bussiness.teachbook.dao.TeachingBookModuleMapper;
import com.xh.bussiness.teachbook.dao.TeachingBookPracticeContentMapper;
import com.xh.bussiness.teachbook.dao.TeachingBookTemplateMapper;
import com.xh.bussiness.teachbook.model.*;
import com.xh.bussiness.teachbook.service.ITeachingBookModuleDetailService;
import com.xh.bussiness.teachbook.service.ITeachingBookModuleService;
import com.xh.bussiness.teachbook.service.ITeachingBookPracticeService;
import com.xh.bussiness.teachbook.service.ITeachingBookService;
import com.xh.bussiness.tiku.bean.dto.ErrorQuestionDto;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/5/13 14:38
 **/
@Service
public class TeachingBookModuleServiceImpl extends BaseServiceImpl<TeachingBookModuleMapper, TeachingBookModule> implements ITeachingBookModuleService {

    public static final Logger log = LoggerFactory.getLogger(TeachingBookPracticeServiceImpl.class);
    @Autowired
    private ITeachingBookModuleDetailService teachingBookModuleDetailService;
    @Autowired
    private ITeachingBookPracticeService teachingBookPracticeService;
    @Autowired
    private TeachingBookTemplateMapper teachingBookTemplateMapper;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private TeachingBookModuleMapper teachingBookModuleMapper;
    @Autowired
    private TeachingBookPracticeContentMapper teachingBookPracticeContentMapper;
    @Autowired
    private ITeachingBookService teachingBookService;
    @Autowired
    private IUserErrorQuestionService userErrorQuestionService;

    @Override
    public List<TeachingBookModuleDto> queryTeachingBookModuleListByParentId(String teachingBookId) {
        TeachingBook teachingBook = teachingBookService.getById(teachingBookId);
        if (teachingBook == null) {
            return new ArrayList<>();
        }
        List<TeachingBookModuleDto> modules = new ArrayList<>();
        List<TeachingBookTemplate> teachingBookTemplates = teachingBookTemplateMapper.queryTeachingBookTemplateByBookId(teachingBookId);
        for (TeachingBookTemplate module : teachingBookTemplates) {
            TeachingBookModuleDto teachingBookModuleDto = new TeachingBookModuleDto();
            teachingBookModuleDto.setId(module.getId());
            teachingBookModuleDto.setName(module.getName());
            teachingBookModuleDto.setType(module.getType());
            teachingBookModuleDto.setOrderNo(module.getOrderNo());

            if (module.getType().equals(TeachingBookConstant.TeachingBookTemplate.PRACTICE)) {
                //练习
                TeachingBookPracticeDto practiceDto = teachingBookPracticeService.getTeachingBookPracticeByTeachingBookId(teachingBookId);
                teachingBookModuleDto.setTeachingBookPracticeRequest(practiceDto);
                modules.add(teachingBookModuleDto);
                continue;
            }
            List<TeachingBookModuleDetail> details = teachingBookModuleDetailService.queryTeachingBookModuleDetailListByParentId(teachingBookId, module.getId());
            if (!CollectionUtils.isEmpty(details)) {
                List<TeachingBookModuleDetailDto> moduleDetailDtos = new ArrayList<>();
                for (TeachingBookModuleDetail detail : details) {
                    TeachingBookModuleDetailDto detailDto = new TeachingBookModuleDetailDto();
                    detailDto.setId(detail.getId());
                    detailDto.setContentType(detail.getContentType());
                    detailDto.setOrderNo(detail.getOrderNo());
                    detailDto.setBlankLine(detail.getBlankLine());
                    detailDto.setTeachingBookModuleId(detail.getTeachingBookModuleId());
                    detailDto.setTeachingBookId(detail.getTeachingBookId());
                    detailDto.setResourceId(detail.getResourceId());
                    detailDto.setSubject(detail.getSubject());
                    detailDto.setId(detail.getId());
                    //错题
                    if (TeachingBookConstant.ModuleContentType.ERROQUESTION.equals(detail.getContentType())) {
                        ErrorQuestionDto errorQuestionDto = new ErrorQuestionDto();
                        UserErrorQuestion userErrorQuestion = userErrorQuestionService.getById(detail.getResourceId());
                        QuestionDto question = questionService.getQuestion(userErrorQuestion.getQuestionId(), null,true,false,false,false,false);
                        if (question == null) {
                            log.error("错题试题不存在" + userErrorQuestion.getQuestionId());
                            continue;
                        }
                        errorQuestionDto.setQuestionDto(question);
                        errorQuestionDto.setErrorQuestion(userErrorQuestion);
                        detailDto.setErrorQuestionDto(errorQuestionDto);
                    } else if (TeachingBookConstant.ModuleContentType.QUESTION.equals(detail.getContentType()) ||
                            TeachingBookConstant.ModuleContentType.VARIABLEQUESTION.equals(detail.getContentType()) ||
                            TeachingBookConstant.ModuleContentType.LIKENESS.equals(detail.getContentType())) {
                        //试题 、 变试题 、相似题
                        QuestionDto question = questionService.getQuestion(detail.getResourceId(), null,true,false,false,false,false);
                        if (question == null) {
                            log.error("试题 、 变试题 、相似题" + detail.getResourceId());
                            continue;
                        }
                        detailDto.setQuestion(question);
                    } else if (TeachingBookConstant.ModuleContentType.EXAMPOINT.equals(detail.getContentType())) {
                        //考点
                        detailDto.setContent(detail.getContent());
                        detailDto.setResourceId(detail.getResourceId());

                    } else if (TeachingBookConstant.ModuleContentType.TEXT.equals(detail.getContentType()) || TeachingBookConstant.ModuleContentType.THINKMAP.equals(detail.getContentType())) {
                        //文本/段落 或者 思维导图
                        detailDto.setContent(detail.getContent());
                        detailDto.setMindMapImgPath(detail.getMindMapImgPath());
                    } else if (TeachingBookConstant.ModuleContentType.MADEQUESTION.equals(detail.getContentType())) {
                        MadeQuestionDto madeQuestionDto = new MadeQuestionDto();
                        //自编题
                        QuestionDto question = questionService.getQuestion(detail.getResourceId(), null,true,false,false,false,false);
                        madeQuestionDto.setQuestionDto(question);
                        detailDto.setMadeQuestionDto(madeQuestionDto);
                    }
                    moduleDetailDtos.add(detailDto);
                }
                moduleDetailDtos = CollectionUtils.isEmpty(moduleDetailDtos) ? new ArrayList<>() : moduleDetailDtos;
                teachingBookModuleDto.setTeachingBookModuleDetailRequest(moduleDetailDtos);
            }
            List<TeachingBookModuleDetailDto> teachingBookModuleDetailRequest = teachingBookModuleDto.getTeachingBookModuleDetailRequest();
            teachingBookModuleDto.setTeachingBookModuleDetailRequest(CollectionUtils.isEmpty(teachingBookModuleDetailRequest) ? new ArrayList<>() : teachingBookModuleDetailRequest);
            modules.add(teachingBookModuleDto);
        }
        return modules;
    }

    @Override
    public List<TeachingBookModuleTree> queryTeachingBookModuleList(String teachingBookId) {
        //1.错题;2.课前回顾;3.知识点讲解与例题详解;4.课堂小结;5.课后练习
        List<TeachingBookModuleTree> teachingBookModuleTrees = teachingBookModuleMapper.queryTeachingBookModule(teachingBookId);

        for (TeachingBookModuleTree teachingBookModuleTree : teachingBookModuleTrees) {
            List<TeachingBookModuleTree> child = new ArrayList<>();
            //课后练习
            if (teachingBookModuleTree.getType().equals(TeachingBookConstant.TeachingBookTemplate.PRACTICE)) {
                List<TeachingBookPracticeContent> practiceContents = teachingBookPracticeContentMapper.selectByTeachingBookId(teachingBookId);
                for (TeachingBookPracticeContent practiceContent : practiceContents) {
                    TeachingBookModuleTree tree = new TeachingBookModuleTree();
                    tree.setId(practiceContent.getId());
                    //1.试题;2.文本
                    if (practiceContent.getContentType().equals(1)) {
                        tree.setName("试题" + practiceContent.getOrderNo());
                    } else {
                        tree.setName("段落" + practiceContent.getOrderNo());
                    }
                    child.add(tree);
                }
                teachingBookModuleTree.setChild(child);
                continue;
            }
            List<TeachingBookModuleDetail> teachingBookModuleDetails =
                    teachingBookModuleDetailService.queryTeachingBookModuleDetailListByParentId(teachingBookId, teachingBookModuleTree.getId());
            for (TeachingBookModuleDetail teachingBookModuleDetail : teachingBookModuleDetails) {
                TeachingBookModuleTree tree = new TeachingBookModuleTree();
                tree.setId(teachingBookModuleDetail.getId());
                String contentTypeName = getContentTypeName(teachingBookModuleDetail.getContentType()) + +teachingBookModuleDetail.getOrderNo();
                tree.setName(contentTypeName);

                child.add(tree);
            }
            teachingBookModuleTree.setChild(child);
        }
        return teachingBookModuleTrees;
    }

    @Override
    public void deleteByTeachingBookId(String teachingBookId) {
        teachingBookModuleMapper.deleteByTeachingBookId(teachingBookId);
    }

    private String getContentTypeName(Integer contentType) {
        if (contentType.equals(TeachingBookConstant.ModuleContentType.ERROQUESTION)) {
            return "错题";
        } else if (contentType.equals(TeachingBookConstant.ModuleContentType.VARIABLEQUESTION)) {
            return "变式";
        } else if (contentType.equals(TeachingBookConstant.ModuleContentType.QUESTION) || contentType.equals(TeachingBookConstant.ModuleContentType.MADEQUESTION)) {
            return "例题";
        } else if (contentType.equals(TeachingBookConstant.ModuleContentType.EXAMPOINT)) {
            return "考点分析";
        } else if (contentType.equals(TeachingBookConstant.ModuleContentType.TEXT)) {
            return "段落";
        } else if (contentType.equals(TeachingBookConstant.ModuleContentType.THINKMAP)) {
            return "思维导图";
        } else if (contentType.equals(TeachingBookConstant.ModuleContentType.LIKENESS)) {
            return "相似题";
        }
        return "其他";
    }

}
